-- SPDX-License-Identifier: GPL-3.0-or-later

local VslUtil = require "packages/VirtualSlash/utility"

local v_chouka = fk.CreateSkill {
  name = "v_chouka",
  tags = {  },
}

v_chouka:addEffect(fk.EventPhaseStart, {
  --赋予摸牌型技能定义
  anim_type = "drawcard",
  --触发条件：触发时机的角色为遍历到的角色、遍历到的角色具有本技能、遍历到的角色处于摸牌阶段。
  can_trigger = function(self, event, target, player, data)
    return (target == player and player:hasSkill(v_chouka.name) and player.phase == Player.Draw)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    --记录已判定的花色
    local used_suit = {}
    local used_suit_show = {}
    local dummy = Fk:cloneCard("slash", Card.NoSuit, 0)
    while true do
      local all_suit = {"spade", "heart", "club", "diamond"}
      for _, s in ipairs(used_suit) do table.removeOne(all_suit, s) end
      local pattern = ".|.|" .. (#all_suit == 0 and "none" or table.concat(all_suit, ","))
      local judge = {
        who = player,
        reason = v_chouka.name,
        pattern = pattern,
      }
      room:judge(judge)
      local suit = judge.card:getSuitString()
      -- local suit_show = judge.card:getSuitString() .. "_char"
      if table.contains(used_suit, suit) then
        return true
      else
        table.insert(used_suit, suit)
        print(VslUtil.card_suits_table(suit))
        table.insert(used_suit_show, VslUtil.card_suits_table(suit))
        dummy:addSubcard(judge.card)
      end
      local prompt_suit = table.concat(used_suit_show, "")
      -- local prompt_suit = used_suit_show
      local prompt = "v_chouka_choice_log:::"..prompt_suit
      -- local prompt = "v_chouka_choice_log1+"..prompt_suit.."+v_chouka_choice_log2"
      local choice = room:askToChoice(player, {
        choices = {"v_chouka_repeat", "v_chouka_stop"},
        skill_name = v_chouka.name,
        prompt = "v_chouka_choice_log:::"..prompt_suit,
      })
      -- local address = "./packages/"..VslUtil.extensionName.."/image/anim/haibao"
      --点击取消，退出判定。
      if choice == "v_chouka_stop" then
        room:obtainCard(player.id, dummy, true)
        -- if #(dummy.subcards) >= 3 then
        --   room:setEmotion(player, address)
        --   --getThread暂无此代码，因此先注释掉。
        --   room:delay(360)
        --   room:setEmotion(player, address)
        --   if #(dummy.subcards) >= 4 then
        --     room:delay(360)
        --     room:setEmotion(player, address)
        --     room:delay(360)
        --     room:setEmotion(player, address)
        --   end
        -- end
        room:delay(500)
        break
      end
    end
  end,
})

return v_chouka